/*
 * 文件名：SpringMVCConfig.java
 * 版权： Copyright 2002-2007 12316yun. All Rights Reserved.
 * 描述：〈描述〉
 
 * 修改人：王钰玺
 * 修改时间：2018/4/26 15:19
 * 修改单号：〈修改单号〉
 * 修改内容：〈修改内容〉
 */
package com.yun12316.framework.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

/**
 * MVC的配置,只扫描@Controller注解,带包名命名,避免混淆同名问题.
 * <p>
 * <p>detailed comment </p>
 *
 * @author 王钰玺 2018/4/26
 * @see
 * @since 1.0
 */
@Configuration
@ComponentScan(
        basePackages="com.yun12316.project.system.*.controller",useDefaultFilters = false,includeFilters= {@ComponentScan.Filter(type= FilterType.ANNOTATION,value=Controller.class)}
 )
public class SpringMVCConfig  implements WebMvcConfigurer  {

    Charset charset= Charset.forName("UTF-8");

    /**
     * 重新注册自定义的消息转化
     */
    @Override
    public   void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
        converters.add(stringHttpMessageConverter());
        converters.add(resourceHttpMessageConverter());
        converters.add(byteArrayHttpMessageConverter());

    }

    /**
     * 上传文件的配置
     * @return
     */

    @Bean
    public CommonsMultipartResolver commonsMultipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver=new CommonsMultipartResolver();
        //默认编码 (ISO-8859-1)
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        //最大内存大小 (10240)
        commonsMultipartResolver.setMaxInMemorySize(10240);
        // 最大文件大小，-1为无限止(-1)
        commonsMultipartResolver.setMaxUploadSize(10485760);
        return commonsMultipartResolver;

    }




    /**
     * 配置静态文件
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/layui/**").addResourceLocations("/layui/");
        registry.addResourceHandler("/upload/**").addResourceLocations("/upload/");
        registry.addResourceHandler("/u/**").addResourceLocations("/u/");
    }


    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter=new MappingJackson2HttpMessageConverter();

        mappingJackson2HttpMessageConverter.setDefaultCharset(charset);
        mappingJackson2HttpMessageConverter.setObjectMapper(new ObjectMapper());

        return mappingJackson2HttpMessageConverter;
    }


    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
        StringHttpMessageConverter stringHttpMessageConverter=new StringHttpMessageConverter(charset);
        return stringHttpMessageConverter;
    }


    @Bean
    public ResourceHttpMessageConverter resourceHttpMessageConverter() {
        ResourceHttpMessageConverter resourceHttpMessageConverter=new ResourceHttpMessageConverter();
        return resourceHttpMessageConverter;
    }

    @Bean
    public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
        ByteArrayHttpMessageConverter byteArrayHttpMessageConverter=new ByteArrayHttpMessageConverter();
        return byteArrayHttpMessageConverter;
    }

}
